from datetime import datetime
from typing import Optional

import yaml
from passlib.context import CryptContext

from src.base.constants import datetime_pattern

# 加密管理器
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")


# 读取 yaml 文件
def load_yaml(file_path: str):
    with open(file_path, 'r', encoding='utf-8') as file:
        return yaml.safe_load(file)


# 密码验证
def verify_password(plain_password, hashed_password):
    return pwd_context.verify(plain_password, hashed_password)


# 密码加密
def hash_password(password):
    return pwd_context.hash(password)


def serialize_datetime(value: Optional[datetime], pattern: str = datetime_pattern):
    if value is None:
        return None
    return value.strftime(pattern)
